import Vue from 'vue'
import Vuex from 'vuex'
import loclaStage from '../utils/loclaStage'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    historySearchList: loclaStage.getList(),
    count: 10,
    test: {
      age: 10,
      name: 'kkw',
    },
  },
  getters: {},
  mutations: {
    ADD_HISTORY_SEARCH_LIST: (state, wordList) => {
      state.historySearchList = wordList
    },
    ADD_COUNT: (state, num) => {
      state.count = state.count + num
    },
    REDUCE_COUNT: (state) => {
      state.count++
    },
  },
  actions: {
    /** 添加首页的本地搜索关键词 **/
    addHistorySearchList(context, word) {
      context.state.historySearchList.push(word)
      let res = context.state.historySearchList
      res = Array.from(new Set(res)) // 数组去重
      if (res.length > 10) { // 只保留10个最新的10个历史数据
        res = res.slice(-10)
      }
      context.commit('ADD_HISTORY_SEARCH_LIST', res)
      loclaStage.setList(res)
    },
  },
  modules: {
  },
})
